package cn.com.lms.business.exam.persistence.repository;

import cn.com.lms.business.exam.persistence.entity.ExerciseQuestion;

import com.bnzj.core.persistence.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

@Repository
public interface ExerciseQuestionRepository extends BaseRepository<ExerciseQuestion,Long> {

    /**
     * 查询练习题库列表
     * @param exerciseId 练习ID
     * @return
     */
    @Query(value = "select eq.id,q.question_name as questionName,eq.question_id as  questionId,q.create_time as createTime , q.creator_name creatorName,qc.category_name as categoryName,(select count(1) from bnz_exam_question_subject qs where qs.is_delete=0 and qs.question_id=eq.question_id ) count   from bnz_exam_exercise_question eq LEFT JOIN  bnz_exam_question q on eq.question_id=q.id left  join bnz_exam_question_category qc on qc.id=q.category_id where eq.is_delete=0 and  eq.exercise_id=:exerciseId  ", nativeQuery = true)
    List<Map> findQuestionByExerciseId(@Param(value = "exerciseId") Long exerciseId);

    @Transactional
    @Modifying
    @Query(value = "delete from bnz_exam_exercise_question where exercise_id=?1", nativeQuery = true)
    public int deleteByExerciseId(Long exerciseId);
}
